package cn.thoughtworks.school.programCenter.repositories;

import cn.thoughtworks.school.programCenter.entities.ReviewQuiz;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;

public interface ReviewQuizRepository extends JpaRepository<ReviewQuiz, Long> {

    @Query(" select sum(t.grade) as grade from ReviewQuiz t where t.studentId = ?1 and t.taskId in ?2")
    Long getScores(Long studentId, List<Long> ids);

    List<ReviewQuiz> findByAssignmentIdAndStudentId(Long assignmentId, Long studentId);

    @Query(" select count(t.id) from ReviewQuiz t where t.studentId = ?1 and t.status!='已提交' and t.taskId in ?2")
    Long getFinishSectionCount(Long studentId, List<Long> taskIds);

    @Query(nativeQuery=true, value = "select t.studentId, sum(t.grade) as grade from ProgramCenter.reviewQuiz t where t.studentId in :studentIds and t.taskId in :taskIds and t.status!='已提交' group by t.studentId order by grade desc")
    List<Map> getScoresLimit(@Param("studentIds") List<Long> studentIds, @Param("taskIds") List<Long> ids);

    @Query(" select t.studentId as studentId, count(t.id) as count from ReviewQuiz t where t.studentId in ?1 and t.status!='已提交' and t.taskId in ?2 group by t.studentId ")
    List<Map> getFinishSectionCountLimit(List<Long> studentIds, List<Long> taskIds);

    ReviewQuiz findByAssignmentIdAndQuizIdAndStudentId(Long assignmentId, Long quizId, Long studentId);

    List<ReviewQuiz> findAllByAssignmentIdAndStudentId(Long assignmentId, Long studentId);

    Page<ReviewQuiz> findAllByAssignmentIdAndQuizIdAndStatus(Long assignmentId, Long quizId, String status, Pageable pageable);

    List<ReviewQuiz> findAllByTaskIdInAndStudentId(List taskIds, Long userId);

    List<ReviewQuiz> findAllByAssignmentIdAndStudentIdIn(Long id, List<Long> studentIds);

    List<ReviewQuiz> findByTaskIdAndAssignmentId(Long taskId, Long assignmentId);

}
